﻿Public Class ToolbarFind
    Private GridTarget As DataGridView
    Private DataSetTarget As DataSet

    Public Sub Initialize_Tools(ds As DataSet, grid As DataGridView)
        DataSetTarget = ds
        GridTarget = grid
        For i = 0 To DataSetTarget.Tables(0).Columns.Count() - 1 Step +1
            cbFind.Items.Add(DataSetTarget.Tables(0).Columns(i).ToString())
        Next
        lblRowCount.Text = "Total Row : " + GridTarget.Rows.Count().ToString()
        cbFind.SelectedIndex = 0
    End Sub

    Public Sub GridTarget_SortChange()
        cbFind.Text = GridTarget.SortedColumn.HeaderCell.Value
        txtFind.Focus()
    End Sub

    Private Sub cbFind_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbFind.SelectedIndexChanged
        Dim searchby As String = cbFind.Items.Item(cbFind.SelectedIndex)
        Dim dataType As String = DataSetTarget.Tables(0).Columns(searchby).DataType.ToString()
        txtFind.Visible = True
        dtFind.Visible = False
        txtFind.Text = ""
        If (dataType = "System.DateTime") Then
            txtFind.Visible = False
            dtFind.Visible = True
        End If
    End Sub

    Private Sub txtFind_TextChanged(sender As Object, e As EventArgs) Handles txtFind.TextChanged
        If (cbFind.Items.Count() > 0) Then
            Dim searchby As String = cbFind.Items.Item(cbFind.SelectedIndex)
            Dim searchvalue As String = txtFind.Text.Trim()
            Dim dvFind As DataView = New DataView(DataSetTarget.Tables(0))
            Dim dataType As String = DataSetTarget.Tables(0).Columns(searchby).DataType.ToString()
            If (dataType = "System.String") Then
                dvFind.RowFilter = "[" & searchby & "] LIKE '%" & searchvalue & "%'"
            ElseIf (dataType = "System.DateTime") Then
                Dim searchDate As DateTime = dtFind.Value
                If (searchvalue = "" Or IsDate(searchvalue) = False) Then
                    dvFind.RowFilter = "[" & searchby & "] = [" & searchby & "]"
                Else
                    searchvalue = searchDate.ToString("yyyy-MM-dd")
                    dvFind.RowFilter = "[" & searchby & "] = '" & searchvalue & "'"
                End If
            ElseIf (dataType = "System.Decimal") Then
                If (txtFind.Text = "") Then
                    dvFind.RowFilter = "[" & searchby & "] = [" & searchby & "]"
                ElseIf (IsNumeric(txtFind.Text.Trim()) = False) Then
                    txtFind.Text = "0"
                    searchvalue = "0"
                Else
                    dvFind.RowFilter = "[" & searchby & "] >= " & searchvalue
                End If
            End If
            If (GridTarget.DataSource Is Nothing) Then
                GridTarget.Rows.Clear()
                Dim ic As Integer = dvFind.Count
                Dim ix As Integer = dvFind.Table.Rows.Count
                For Each row As DataRowView In dvFind
                    GridTarget.Rows.Add(row.Row.ItemArray)
                Next row
            Else : GridTarget.DataSource = dvFind
            End If
            lblRowCount.Text = "Total Row : " + GridTarget.Rows.Count().ToString()
        End If
    End Sub

    Private Sub dtFind_ValueChanged(sender As Object, e As EventArgs) Handles dtFind.ValueChanged
        txtFind.Text = dtFind.Value
    End Sub
End Class
